<template>
  <el-dialog
    :title="!houses.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="houses" :rules="dataRule" ref="houses" @keyup.enter.native="housesSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="uid">
      <el-input v-model="houses.uid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="小区" prop="community">
      <el-input v-model="houses.community" placeholder="小区"></el-input>
    </el-form-item>
    <el-form-item label="房产类型" prop="type">
      <el-input v-model="houses.type" placeholder="房产类型"></el-input>
    </el-form-item>
    <el-form-item label="房子面积" prop="area">
      <el-input v-model="houses.area" placeholder="房子面积"></el-input>
    </el-form-item>
    <el-form-item label="房子预算" prop="budget">
      <el-input v-model="houses.budget" placeholder="房子预算"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="houses.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="desc">
      <el-input v-model="houses.desc" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="联系人手机号" prop="phone">
      <el-input v-model="houses.phone" placeholder="联系人手机号"></el-input>
    </el-form-item>
    <el-form-item label="求房类型0/求租1/求购2" prop="requestType">
      <el-input v-model="houses.requestType" placeholder="求房类型0/求租1/求购2"></el-input>
    </el-form-item>
    <el-form-item label="添加时间" prop="createdAt">
      <el-input v-model="houses.createdAt" placeholder="添加时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updatedAt">
      <el-input v-model="houses.updatedAt" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="删除时间" prop="deletedAt">
      <el-input v-model="houses.deletedAt" placeholder="删除时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="housesSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        houses: {
          id: 0,
          uid: '',
          community: '',
          type: '',
          area: '',
          budget: '',
          title: '',
          desc: '',
          phone: '',
          requestType: '',
          createdAt: '',
          updatedAt: '',
          deletedAt: ''
        },
        dataRule: {
          uid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          community: [
            { required: true, message: '小区不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '房产类型不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '房子面积不能为空', trigger: 'blur' }
          ],
          budget: [
            { required: true, message: '房子预算不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          desc: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系人手机号不能为空', trigger: 'blur' }
          ],
          requestType: [
            { required: true, message: '求房类型0/求租1/求购2不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          deletedAt: [
            { required: true, message: '删除时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.houses.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['houses'].resetFields()
          if (this.houses.id) {
            this.$http({
              url: this.$http.adornUrl(`/broker/record/info/${this.houses.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.houses.uid = data.record.uid
                this.houses.community = data.record.community
                this.houses.type = data.record.type
                this.houses.area = data.record.area
                this.houses.budget = data.record.budget
                this.houses.title = data.record.title
                this.houses.desc = data.record.desc
                this.houses.phone = data.record.phone
                this.houses.requestType = data.record.requestType
                this.houses.createdAt = data.record.createdAt
                this.houses.updatedAt = data.record.updatedAt
                this.houses.deletedAt = data.record.deletedAt
              }
            })
          }
        })
      },
      // 表单提交
      housesSubmit () {
        this.$refs['houses'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/broker/record/${!this.houses.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.houses.id || undefined,
                'uid': this.houses.uid,
                'community': this.houses.community,
                'type': this.houses.type,
                'area': this.houses.area,
                'budget': this.houses.budget,
                'title': this.houses.title,
                'desc': this.houses.desc,
                'phone': this.houses.phone,
                'requestType': this.houses.requestType,
                'createdAt': this.houses.createdAt,
                'updatedAt': this.houses.updatedAt,
                'deletedAt': this.houses.deletedAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
